import Vue from "vue";
import Router from "vue-router";
/* 引入页面 */
import Pages from "./Pages";

Vue.use(Router);

var routes = [];
routes = routes.concat(Pages);
routes.push({
  path: "*",
  redirect: {
    name: "homepage_Index"
  }
});

var router = new Router({
  mode: "history",
  routes
});

// 通过Vue的原型注册一个方法
// 让所有实例共享这个方法 
Vue.prototype.$routerPush = ($e, url) => {
  $e.$router.push({
    path: url,
  })
}

router.beforeEach((to, from, next) => {
  // if (from.name) {
  //   window.location.reload();
  // }
  /* 路由发生变化修改页面title */
  if (to.matched.length > 0 && to.matched[0].components.default.title) {
    document.title = to.matched[0].components.default.title;
  }
  /* 路由发生变化修改页面body的class */
  if (to.matched.length > 0) {
    var bodyClass = to.matched[0].components.default.bodyClass;
    if (bodyClass) {
      document.getElementsByTagName("body")[0].className = bodyClass;
    } else {
      document.getElementsByTagName("body")[0].className = "";
    }
  }

  //页面滚动到顶部
  // window.scrollTo(0, 0);
  next();
});

export default router;
